package com.chookss.login;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chookss.MyApp;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.tools.MyToast;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.OkGoConfig;
import com.chookss.tools.okgoconfig.OkGoUtil;
import com.chookss.tools.okgoconfig.callback.JsonCallback;
import com.chookss.tools.old.SignUtils;
import com.lvgroup.hospital.base.BaseAct;
import com.lzy.okgo.model.Response;

import org.jetbrains.annotations.Nullable;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SetNewPasswordActivity extends BaseAct {

    @BindView(R.id.common_title_txt)
    TextView commonTitleTxt;
    @BindView(R.id.edPassword)
    EditText edPassword;
    @BindView(R.id.edPassword2)
    EditText edPassword2;
    private String name, phone, authcode;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_pass);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        initState();
        commonTitleTxt.setText("设置密码");
        name = getIntent().getStringExtra("name");
        phone = getIntent().getStringExtra("phone");
        authcode = getIntent().getStringExtra("authcode");

    }
    private void login() {
        if (Utils.isNull(edPassword.getText().toString().replace(" ", ""))) {
            MyToast.show("新密码不能为空");
            return;
        }
        if (Utils.isNull(edPassword2.getText().toString().replace(" ", ""))) {
            MyToast.show("再次输入密码确认");
            return;
        }
        if (!edPassword.getText().toString().equals(edPassword2.getText().toString())) {
            MyToast.show("两次密码输入不一致");
            return;
        }
//        if (tvPassword.getText().toString().length() < 8) {
//            MyToast.show("密码不能少于8位");
//        }
//        String encodePassword = MD5.INSTANCE.encode(tvPassword.getText().toString());
        Map<String, Object> map = new HashMap<>();
        map.put("companyName",name);
        map.put("account", phone);
        map.put("smsCode", authcode);
        map.put("onePwd", edPassword.getText().toString());
        map.put("twoPwd", edPassword2.getText().toString());
        String timeStamp = SignUtils.getTimeStamp();//20201208125238
        String nonce = SignUtils.getRandomString(14);
        String makeSignStr = SignUtils.makeSignStr(map, timeStamp, nonce, true);
        Map<String, String> maps = new HashMap<>();
        maps.put("companyName",name);
        maps.put("account",phone);
        maps.put("smsCode", authcode);
        maps.put("onePwd", edPassword.getText().toString());
        maps.put("twoPwd", edPassword2.getText().toString());
        maps.put("timestamp", timeStamp);
        maps.put("nonce", nonce);
        maps.put("sign", makeSignStr);
        showLoading();
//        OkGoConfig.init(MyApp.appCtx, "login",
//                Utils.getVersionName(SetNewPasswordActivity.this));
        MyHttpRequest.postRequest(Urls.resetPwd, this, maps, new JsonCallback<String>() {
            @Override
            public void onSuccess(Response<String> response) {
                dismissLoading();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(response.body().toString());
                    if ("200".equals(jsonObject.getString("code"))) {
                        MyToast.show(jsonObject.getString("msg"));
                        finish();
                    } else {
                        if (jsonObject.getString("code").startsWith("400")) {
                            OkGoUtil.onErrorIntent(jsonObject.getString("code"),jsonObject.getString("msg"));
                        }else{
                            MyToast.show(jsonObject.getString("msg"));
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    @OnClick({R.id.common_title_back, R.id.tvSure})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_title_back:
                finish();
                break;
            case R.id.tvSure:
                login();
                break;
        }
    }
}
